package com.imay.live.fragment.discovery;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.imay.live.R;
import com.imay.live.cache.DiscoveryCache;
import com.imay.live.fragment.BaseRequestFragment;
import com.imay.live.heartlikeslib.scrollablelayoutlib.ScrollableHelper;
import com.imay.live.model.BannerInfoModel;
import com.imay.live.model.FeedModel;
import com.imay.live.view.custom.GameTitleView;
import com.imay.live.view.recyclerview.WrapContentLinearLayoutManager;
import com.imay.live.view.refresh.RefreshView;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.footer.LoadingView;

import java.util.ArrayList;

/**
 * Created by liangzhenxiong on 2017/04/11 0011.
 * 发现的公共类
 */

public class LoadFragment extends BaseRequestFragment implements ScrollableHelper.ScrollableContainer {
    int tabH;//主页的TAB 高度
    public RecyclerView mLinearRecyclerView;
    public TwinklingRefreshLayout mRefreshLayout;
    View load_layout;
    ImageView progress;
    TextView load_text;
    int mpapecount;//当前数据的条数
    //    private Animation mRotateAnimation;
    ArrayList<MultiItemEntity> mdatas;
    public int postion;
    //    SliderBannerManager mBannerManager;
    BaseQuickAdapter mAdapter;
    protected ArrayList<BannerInfoModel> bannerInfoModels;
    protected ArrayList<FeedModel> feedSuggestModels;
    //    EndlessRecyclerOnScrollListener scrollListener;
    boolean needmore;
    View tabEmptyView;

    protected ArrayList<String> hotLabels;

    protected GameTitleView gameTitleTextView;



    /**
     * 给底部添加一个白色的区域
     *
     * @param tabH
     */
    void addFooterView(int tabH) {
//        if (tabH > 0 && mAdapter != null && tabEmptyView == null && mAdapter.getData().size() > 0) {
//            tabEmptyView = new View(getActivity());
////            tabEmptyView.setBackgroundColor(getResources().getColor(R.color.bg_f5));
//            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, tabH);
//            tabEmptyView.setLayoutParams(params);
//            mAdapter.addFooterView(tabEmptyView);
//        }
    }

//    public void setpostion(int postion) {
//        this.postion = postion;
//        if (mBannerManager != null)
//            mBannerManager.Playto(postion);
//    }

    public void setTabH(int tabH) {
        this.tabH = tabH;
    }


    public void onRefresh() {

    }

    public void onLoadMore() {

    }

    public boolean getsetEnableRefresh() {
        return true;
    }

    public void updateFeedSuggest(ArrayList<FeedModel> feedSuggestModel) {
        this.feedSuggestModels = feedSuggestModel;
    }

    public void updateLabels(ArrayList<String> hotLabels){
        this.hotLabels = hotLabels;
    }


//    public void updateBanners(ArrayList<BannerInfoModel> bannerInfoModels, int index) {
//        this.bannerInfoModels = bannerInfoModels;
//        if (mBannerManager == null) {
//            return;
//        }
//        if (index < 0) {
//            index = postion;
//        }
//        mBannerManager.play(bannerInfoModels, new SliderBannerManager.OnAdClickListener() {
//            @Override
//            public void onClick(BannerInfoModel bannerInfoModel) {//accessToken  uid
//                goWebBanner(bannerInfoModel);
//
//            }
//        }, index);
//    }

    public void addbanner() {
//        SliderBanner mBannerView = (SliderBanner) LayoutInflater.from(getContext()).inflate(R.layout.banner_layout, mLinearRecyclerView, false);
//        mBannerManager = new SliderBannerManager(mBannerView);
//        int viewHeight = (ViewUtil.mScreemWidth * 340) / 1080;
//        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, viewHeight);
//        mBannerView.setLayoutParams(layoutParams);
//        if (bannerInfoModels != null) {
//            mBannerManager.play(bannerInfoModels, new SliderBannerManager.OnAdClickListener() {
//                @Override
//                public void onClick(BannerInfoModel bannerInfoModel) {
//                    goWebBanner(bannerInfoModel);
//                }
//            }, postion);
//        }
//        mAdapter.addHeaderView(mBannerView);
        View view = new View(getContext());
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DiscoveryFragment.getviewHeight());
        view.setLayoutParams(layoutParams);
        view.setBackgroundColor(Color.TRANSPARENT);
        mAdapter.addHeaderView(view);
    }

    void addLoadLayout(RecyclerView recyclerView) {
        if (load_layout == null) {
            load_layout = LayoutInflater.from(getContext()).inflate(R.layout.load_layout, recyclerView, false);
//            load_layout.setBackgroundColor(0);
            mAdapter.addFooterView(load_layout);
//            int h = mRefreshLayout.getMeasuredHeight() - mLinearRecyclerView.getMeasuredHeight();
            load_layout.setPadding(0, 300, 0, 0);
            progress = (ImageView) load_layout.findViewById(R.id.progress_icon);
            load_text = (TextView) load_layout.findViewById(R.id.load_text);
            load_layout.setVisibility(View.VISIBLE);
            ((AnimationDrawable) progress.getDrawable()).start();
        }

    }

    void completeLoad(boolean loadMoreEnd) {
        if (load_layout != null) {
            load_layout.setVisibility(View.GONE);
            progress.clearAnimation();
        }
        if (mRefreshLayout != null) {
            mRefreshLayout.finishRefreshing();
            mRefreshLayout.setEnableLoadmore(!loadMoreEnd);
        }
    }

    void completeLoadmore(boolean loadMoreEnd) {
        if (mRefreshLayout != null) {
            mRefreshLayout.finishLoadmore();
            mRefreshLayout.setEnableLoadmore(!loadMoreEnd);
        }
    }

    void completeLoad() {
        completeLoad(false);
    }

    void loadError(String error) {
        mAdapter.getData().clear();
        mAdapter.notifyDataSetChanged();
        if (load_layout != null) {
            load_text.setVisibility(View.VISIBLE);
            load_layout.setVisibility(View.VISIBLE);
            load_text.setText(error);
            progress.clearAnimation();
            progress.setImageResource(R.mipmap.wushuju);
        }
        if (gameTitleTextView!=null){
            gameTitleTextView.setViewVisible(false);
        }
        if (mRefreshLayout != null) {
            mRefreshLayout.finishRefreshing();
            mRefreshLayout.finishLoadmore();
        }
    }

    void loadmoreError(String error) {
        if (load_layout != null) {
            load_layout.setVisibility(View.GONE);
            progress.clearAnimation();
        }
        if (mRefreshLayout != null) {
            mRefreshLayout.finishRefreshing();
            mRefreshLayout.finishLoadmore();
        }

    }


    public void setRefreshLayout(final Activity activity, TwinklingRefreshLayout mRefreshLayout) {
        this.mRefreshLayout = mRefreshLayout;
        RefreshView headerView = new RefreshView(activity);
        LoadingView bottomProgressView = new LoadingView(activity);
        mRefreshLayout.setHeaderView(headerView);
        mRefreshLayout.setBottomView(bottomProgressView);
        mRefreshLayout.setEnableLoadmore(true);
        mRefreshLayout.setEnableOverScroll(false);
        mRefreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(final TwinklingRefreshLayout refreshLayout) {
                DiscoveryCache.onRefresh(LoadFragment.this, activity);
                LoadFragment.this.onRefresh();
            }

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                LoadFragment.this.onLoadMore();
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dis_content;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mLinearRecyclerView = $(R.id.rcv_content);
        if (mRefreshLayout != null) {
            mRefreshLayout.setChildView(mLinearRecyclerView);

        }
        WrapContentLinearLayoutManager wrapContentLinearLayoutManager
                = new WrapContentLinearLayoutManager(getActivity());
        mLinearRecyclerView.setLayoutManager(wrapContentLinearLayoutManager);
        mLinearRecyclerView.setHasFixedSize(true);
        mdatas = new ArrayList<MultiItemEntity>();
//        scrollListener = new EndlessRecyclerOnScrollListener() {
//            @Override
//            public void onLoadMore(boolean enableRefresh, int currentPage) {
//                ((MainActivity) getActivity()).setEnableLoadmore(enableRefresh);
//            }
//
//            @Override
//            public void setEnableRefresh(boolean enableRefresh) {
//                ((MainActivity) getActivity()).setEnableRefresh(enableRefresh);
//            }
//        };
//        mLinearRecyclerView.addOnScrollListener(scrollListener);
        mpapecount = 0;
        initAdapte();
        addbanner();
        addLoadLayout(mLinearRecyclerView);
        oncreate();
        DiscoveryCache.oncreate(this, getActivity());
    }

    void oncreate() {
    }

    void initAdapte() {

    }

    @Override
    public RecyclerView getScrollableView() {
        return mLinearRecyclerView;
    }

//    public int getPlayPostion() {
//        return mBannerManager.getPlayPostion();
//    }
//
//    public void stopPlay() {
//        mBannerManager.stopPlay();
//    }

    @Override
    public void onResume() {
        super.onResume();
        if (getScrollableView() != null) {
            if (getUserVisibleHint() && mRefreshLayout != null
                    ) {
                mRefreshLayout.setChildView(getScrollableView());
            }
        }
        DiscoveryCache.onResume(this, getActivity());
        setVisibleHint(true);
    }

    @Override
    public void onPause() {
        super.onPause();
//        stopPlay();
        setVisibleHint(false);
    }

    public void setVisibleHint(boolean isVisible) {
    }
}
